package com.qigao.mall.web.controller.admin.merchant;

import com.github.pagehelper.PageInfo;
import com.qigao.mall.api.WithDrawService;
import com.qigao.mall.commons.dto.*;
import com.qigao.mall.commons.enums.Result;
import com.qigao.mall.commons.enums.ResultCode;
import com.qigao.mall.commons.exceptions.BusinessException;
import com.qigao.mall.commons.utils.excel.ExcelHeaderInfo;
import com.qigao.mall.commons.utils.excel.ExcelUtils;
import com.qigao.mall.web.controller.AbstractExcelController;
import com.qigao.mall.web.utils.RequestUtils;
import com.qigao.mall.web.utils.ValidatorUtil;
import com.qigao.mall.web.vo.PageVo;
import com.qigao.mall.web.vo.app.OrderQueryVo;
import com.qigao.mall.web.vo.app.WithDrawApplyVo;
import com.qigao.mall.web.vo.app.WithDrawAuditVo;
import com.qigao.mall.web.vo.app.WithDrawQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author luiz
 * @Title: com.qigao.mall.web.controller.app
 * @ProjectName three-mall
 * @date 2020-06-2020-06-12 10:39
 * @Description:
 */
@RestController
@RequestMapping("/admin/withDraw/")
@Api(tags = "Admin提现相关")
public class AdminWithDrawController extends AbstractExcelController {
    private static Logger logger= LoggerFactory.getLogger(AdminWithDrawController.class);
    @Resource
    WithDrawService withDrawService;

    //1.提现审核
    @ApiOperation("提现审核")
    @PostMapping("/aduit")
    public Result auditWithDraw(@RequestBody WithDrawAuditVo withDrawAuditVo, HttpServletRequest request){
        ValidatorUtil.validateEntity(withDrawAuditVo);
        try{
            WithDrawAuditDto withDrawAuditDto=new WithDrawAuditDto();
            BeanUtils.copyProperties(withDrawAuditVo,withDrawAuditDto);
            withDrawAuditDto.setUserNo(RequestUtils.getCurrentUserNo(request));
            return withDrawService.withDrawAudit(withDrawAuditDto);
        }catch (BusinessException be){
            logger.error("提现申请异常:{}",be);
            return Result.newError(be.getCode(),be.getMsg());
        }catch (Exception e){
            logger.error("提现申请发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }
    //2.查询提现单列表
    @PostMapping("/listPage")
    public Result<PageInfo<WithDrawOrderResultDto>> listPage(@RequestBody WithDrawQueryVo withDrawQueryVo, HttpServletRequest request){
        try{
            WithDrawQueryDto withDrawQueryDto=new WithDrawQueryDto();
            BeanUtils.copyProperties(withDrawQueryVo,withDrawQueryDto);
            if(StringUtils.isNotEmpty(withDrawQueryVo.getStartDate())){
                withDrawQueryDto.setStartTime(withDrawQueryVo.getStartDate()+" 00:00:00");
            }
            if(StringUtils.isNotEmpty(withDrawQueryVo.getEndDate())){
                withDrawQueryDto.setEndTime(withDrawQueryVo.getEndDate()+" 23:59:59");
            }
            return withDrawService.findWithDrawPage(withDrawQueryDto);
        }catch (Exception e){
            logger.error("提现单列表查询发生异常:{}",e);
            return Result.newError(ResultCode.FAIL);
        }
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void exportGet(WithDrawQueryVo withDrawQueryVo, HttpServletResponse response) {
        WithDrawQueryDto withDrawQueryDto=new WithDrawQueryDto();
        BeanUtils.copyProperties(withDrawQueryVo,withDrawQueryDto);
        if(StringUtils.isNotEmpty(withDrawQueryVo.getStartDate())){
            withDrawQueryDto.setStartTime(withDrawQueryVo.getStartDate()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(withDrawQueryVo.getEndDate())){
            withDrawQueryDto.setEndTime(withDrawQueryVo.getEndDate()+" 23:59:59");
        }
        // 待导出数据
        List<WithDrawOrderResultDto> withDrawOrderResultDtos = withDrawService.findOrderResultByCond(withDrawQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(withDrawOrderResultDtos, getHeaderInfo(), null);
        sendHttpResponse(response, "提现报表", excelUtils.getWorkbook());
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出Excel", notes = "导出Excel")
    public void export(@RequestBody WithDrawQueryVo withDrawQueryVo, HttpServletResponse response) {
        WithDrawQueryDto withDrawQueryDto=new WithDrawQueryDto();
        BeanUtils.copyProperties(withDrawQueryVo,withDrawQueryDto);
        if(StringUtils.isNotEmpty(withDrawQueryVo.getStartDate())){
            withDrawQueryDto.setStartTime(withDrawQueryVo.getStartDate()+" 00:00:00");
        }
        if(StringUtils.isNotEmpty(withDrawQueryVo.getEndDate())){
            withDrawQueryDto.setEndTime(withDrawQueryVo.getEndDate()+" 23:59:59");
        }
        // 待导出数据
        List<WithDrawOrderResultDto> withDrawOrderResultDtos = withDrawService.findOrderResultByCond(withDrawQueryDto);
        ExcelUtils excelUtils = new ExcelUtils(withDrawOrderResultDtos, getHeaderInfo(), null);
        sendHttpResponse(response, "提现报表", excelUtils.getWorkbook());
    }


    // 获取表头信息
    private List<ExcelHeaderInfo> getHeaderInfo() {
        return Arrays.asList(
                new ExcelHeaderInfo(0, 0, 0, 0, "申请时间"),
                new ExcelHeaderInfo(0, 0, 1, 1, "提现金额"),
                new ExcelHeaderInfo(0, 0, 2, 2, "申请人"),
                new ExcelHeaderInfo(0, 0, 3, 3, "提现状态")
        );
    }

}
